//
//  LogViewController.m
//  Manage
//
//  Created by 杨康 on 2017/10/30.
//  Copyright © 2017年 杨康. All rights reserved.
//

#import "LogViewController.h"
#import "ForgetPasswordViewController.h"
#import "RegViewController.h"
#import "UserModel.h"
@interface LogViewController ()
{
    YKTextField *_phoneTF;
    YKTextField *_passTF;
}
@end

@implementation LogViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden=NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    
    
    //登录背景图
    UIImageView *logBgImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"登录背景图"]];
    [self.view addSubview:logBgImageView];
    
    [logBgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(100);
    }];
    
    
    
    
    

    //手机号
    YKTextField *phoneTF=[SingLeton myTextFieldsecure:NO andkeyboardType:UIKeyboardTypeNumberPad andplaceholder:@"请输入手机号" andLeftImage:@"手机号" RequestSuccess:^(YKTextField *textField, NSRange range, NSString *string) {
        
    }];
    [self.view addSubview:phoneTF];
    
    [phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(logBgImageView.mas_bottom).mas_offset(45);
        make.left.mas_equalTo(35);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-70, 44));
    }];
    _phoneTF=phoneTF;
    phoneTF.backgroundColor=[UIColor colorWithHexString:@"f5f5f5"];
    
    
    //密码
    YKTextField *passTF=[SingLeton myTextFieldsecure:YES andkeyboardType:UIKeyboardTypeDefault andplaceholder:@"请输入登录密码" andLeftImage:@"密码" RequestSuccess:^(YKTextField *textField, NSRange range, NSString *string) {
        
    }];
    [self.view addSubview:passTF];
    
    [passTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(phoneTF.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(phoneTF);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-70, 44));
    }];
    
    _passTF=passTF;
    passTF.backgroundColor=[UIColor colorWithHexString:@"f5f5f5"];
    
    
    //登录按钮
    UIButton  *logButton=[SingLeton endButton:[UIColor colorWithHexString:@"09be00"] andTitle:@"登录" RequestSuccess:^(UIButton *btn) {
        
        [UDF setObject:@"9999" forKey:@"token"];
        AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [delegate switchController];
//        [self loginAction];
    }];
    [self.view addSubview:logButton];
    [logButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(passTF.mas_bottom).mas_offset(20);
        make.centerX.mas_equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-70, 44));
    }];
    
    
    //用户注册
    UIButton * regButton=[SingLeton endButton:nil andTitle:@"用户注册" RequestSuccess:^(UIButton *btn) {
        RegViewController *vc=[[RegViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [self.view addSubview:regButton];
    [regButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(logButton.mas_bottom).mas_offset(30);
        make.left.mas_equalTo(logButton);
    }];
    regButton.titleLabel.font=[UIFont systemFontOfSize:16];
    [regButton setTitleColor:[UIColor colorWithHexString:@"09be00"] forState:UIControlStateNormal];
    
    
    
    
    
    //忘记密码
    UIButton * forgetButton=[SingLeton endButton:nil andTitle:@"忘记密码" RequestSuccess:^(UIButton *btn) {
        ForgetPasswordViewController *vc=[[ForgetPasswordViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [self.view addSubview:forgetButton];
    [forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(logButton.mas_bottom).mas_offset(30);
        make.right.mas_equalTo(logButton);
        
    }];
    forgetButton.titleLabel.font=[UIFont systemFontOfSize:16];
    [forgetButton setTitleColor:[UIColor color9] forState:UIControlStateNormal];

    
    
    
    
    
}
-(void)loginAction
{
    [UDF removeObjectForKey:@"token"];
    
    NSString *URLString = @"/axq/admin/login";
    NSDictionary *parameters = @{@"loginid": _phoneTF.text, @"password": _passTF.text};
    [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
        LRLog(@"%@",responseObject);
        NSString *code = responseObject[@"code"];
        if([code isEqualToString:@"10005"]){
            NSDictionary *data=responseObject[@"data"];
            UserModel *model=[UserModel new];
            model.addtime=[NSString stringWithFormat:@"%@",data[@"addtime"]];
            model.updatetime=[NSString stringWithFormat:@"%@",data[@"updatetime"]];
            model.lat=[NSString stringWithFormat:@"%@",data[@"lat"]];
            model.lon=[NSString stringWithFormat:@"%@",data[@"lon"]];
            model.password=[NSString stringWithFormat:@"%@",data[@"password"]];
            model.telephone=[NSString stringWithFormat:@"%@",data[@"telephone"]];
            model.userCode=[NSString stringWithFormat:@"%@",data[@"userCode"]];
            model.userRole=[NSString stringWithFormat:@"%@",data[@"userRole"]];
            model.userTitle=[NSString stringWithFormat:@"%@",data[@"userTitle"]];
            model.address=[NSString stringWithFormat:@"%@",data[@"address"]];
            model.contacts=[NSString stringWithFormat:@"%@",data[@"contacts"]];
            model.headimg=[NSString stringWithFormat:@"%@",data[@"headimg"]];
            model.someId=[NSString stringWithFormat:@"%@",data[@"id"]];
            model.linkman=[NSString stringWithFormat:@"%@",data[@"linkman"]];
            model.loginId=[NSString stringWithFormat:@"%@",data[@"loginId"]];
            model.status=[NSString stringWithFormat:@"%@",data[@"status"]];
            
            [UDF setObject:model.userRole forKey:@"token"];
            AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [delegate switchController];
            
        }else{
            NSString *str=responseObject[@"msg"];
            
            [self.view makeToast:str];
        }
        
        
    } RequestFailed:^(NSError *error) {
        
    }];
}




@end
